package com.bjpowernode.dataservice.service;

import com.bjpowernode.api.model.FinanceAccount;
import com.bjpowernode.api.model.User;
import com.bjpowernode.api.pojo.UserAccountInfoDto;
import com.bjpowernode.api.service.ProductService;
import com.bjpowernode.api.service.UserService;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.dataservice.mapper.FinanceAccountMapper;
import com.bjpowernode.dataservice.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@DubboService(interfaceClass = UserService.class,version = "1.0")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private FinanceAccountMapper financeAccountMapper;

    @Value("${ylb.config.password-salt}")
    private String salt;

    @Override
    public User queryByPhone(String phone) {
        User user = null;
        //校验参数
        if (CommonUtil.checkPhone(phone)){
            //合法
            user = userMapper.selectByPhone(phone);
        }
        return user;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized int userRegister(String phone, String password) {
        int result = 0;
        //检查参数
        if (CommonUtil.checkPhone(phone)&& password != null && password.length()>=6 && password.length()<=32){
            //查询手机号码是否已经存在
            if (userMapper.selectByPhone(phone)==null){
                //加工密码，加盐，md5加密
                String newPassword = DigestUtils.md5DigestAsHex((password + salt).getBytes());
                //注册user
                User user = new User();
                user.setPhone(phone);
                user.setLoginPassword(newPassword);
                user.setAddTime(new Date());
                userMapper.insertReturnPrimaryKey(user);
                //获取user.getId()
                FinanceAccount account = new FinanceAccount();
                account.setUid(user.getId());
                account.setAvailableMoney(new BigDecimal("0"));
                financeAccountMapper.insertSelective(account);
                //成功
                result = 1;
            }else{
                //2表示手机号已经存在
                result = 2;
            }
        }
        return result;
    }

    @Override
    @Transactional
    public User loginUser(String phone, String pword) {
        User user = null;
        if (CommonUtil.checkPhone(phone)&& pword != null && pword.length()==32){
            String newPassword = DigestUtils.md5DigestAsHex((pword + salt).getBytes());
            user = userMapper.selectLogin(phone, newPassword);
            //更新最后的登录时间
            if (user!=null){
                user.setLastLoginTime(new Date());
                userMapper.updateByPrimaryKeySelective(user);
            }

        }
        return user;
    }

    @Override
    public boolean modifyRealName(String phone, String name, String idCard) {
        int i = 0;
        if (!StringUtils.isAnyBlank(phone,name,idCard)){
            i = userMapper.updateRealname(phone,name,idCard);
        }
        return i>0;
    }

    @Override
    public UserAccountInfoDto queryUserAllInfo(Integer uid) {
        UserAccountInfoDto info = null;
        //判断参数
        if(uid != null && uid >0){
            info = userMapper.queryUserAllById(uid);
        }
        return info;
    }

    /*查询用户*/
    @Override
    public User queryById(Integer uid) {
        User user = null;
        if (uid != null && uid >0){
            user = userMapper.selectByPrimaryKey(uid);
        }
        return user;
    }
}
